package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Singer;
import com.javaclimb.music.service.SingerService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/singer")
public class SingerController {
    @Autowired
    private SingerService singerService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addSinger(HttpServletRequest request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format1.parse(request.getParameter("birth"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Singer singer = new Singer();
        singer.setName(request.getParameter("name").trim());
        singer.setSex(Integer.parseInt(request.getParameter("sex")));
        singer.setPic(request.getParameter("pic"));
        singer.setBirth(date);
        singer.setLocation(request.getParameter("location").trim());
        singer.setIntroduction(request.getParameter("introduction").trim());
        boolean isRight = singerService.insert(singer);
        if (isRight) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "添加失败");
        return jsonObject;
    }

    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Object selectAll(HttpServletRequest request, HttpSession session) {
        List<Singer> list = singerService.allSinger();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Consts.SINGLIST, list);
        return jsonObject;
    }

    @RequestMapping(value = "/updateSinger", method = RequestMethod.POST)
    public Object updateSinger(HttpServletRequest request, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format1.parse(request.getParameter("birth"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Singer singer = new Singer();
        singer.setId(Integer.parseInt(request.getParameter("id")));
        singer.setName(request.getParameter("name").trim());
        singer.setSex(Integer.parseInt(request.getParameter("sex")));
        singer.setPic(request.getParameter("pic"));
        singer.setBirth(date);
        singer.setLocation(request.getParameter("location").trim());
        singer.setIntroduction(request.getParameter("introduction").trim());
        boolean isRight = singerService.update(singer);
        if (isRight) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteSinger", method = RequestMethod.GET)
    public Object deleteSinger(HttpServletRequest request, HttpSession session) {
        Integer id = Integer.parseInt(request.getParameter("id"));
        boolean isRight = singerService.delete(id);
        JSONObject jsonObject = new JSONObject();
        if (isRight) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }

    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public Object selectById(HttpServletRequest request, HttpSession session) {
        Integer id = Integer.parseInt(request.getParameter("id"));
        Singer singer = singerService.selectByPrimaryKey(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Consts.SINGER, singer);
        return jsonObject;
    }

    @RequestMapping(value = "/selectByName", method = RequestMethod.GET)
    public Object selectByName(HttpServletRequest request, HttpSession session) {
        String name = request.getParameter("name");
        List<Singer> list = singerService.singerOfName(name);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Consts.SINGLIST, list);
        return jsonObject;
    }

    @RequestMapping(value = "/selectBySex", method = RequestMethod.GET)
    public Object selectBySex(HttpServletRequest request, HttpSession session) {
        Integer sex = Integer.parseInt(request.getParameter("sex"));
        List<Singer> list = singerService.singerOfSex(sex);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Consts.SINGLIST, list);
        return jsonObject;
    }

    @RequestMapping(value = "/postUpdate", method = RequestMethod.POST)
    public Object postUpdate(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws IOException {
        String fileName = file.getOriginalFilename();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateNow = sdf.format(date);
        //文件名
        String nowFileName = dateNow + fileName;
        //路径
        String path = System.getProperty("user.dir") + System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + "singerPic";
        File dir = new File(path);
        //如果没有路径创建
        if (!dir.exists()) {
            dir.mkdirs();
        }
        //实际路径
        File dest = new File(path + System.getProperty("file.separator") + nowFileName);
        String sqlDir = "/img/singerPic/" + nowFileName;
        file.transferTo(dest);
        Singer singer = new Singer();
        singer.setId(id);
        singer.setPic(sqlDir);
        JSONObject jsonObject = new JSONObject();
        boolean isRight = singerService.update(singer);
        if (isRight) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }

    @RequestMapping(value = "/deleteAll", method = RequestMethod.GET)
    public Object deleteAllSinger(HttpServletRequest request, HttpSession session) {
        String id = request.getParameter("id");
        String[] strings = id.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(Integer.parseInt(strings[i]));
        }
        int a = singerService.deleteAll(list);
        JSONObject jsonObject = new JSONObject();
        if (a > 0) {
            jsonObject.put(Consts.CODE, 1);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        return jsonObject;
    }
}
